package com.demo.thread.sync;

/**
 * @author : YU.TAN
 * @date : 2021/11/4 13:53
 */
public class Race implements Runnable{

    private String winner;

    @Override
    public void run() {
        for (int i = 0; i <=100 ; i++) {

        if ("兔子".equals(Thread.currentThread().getName())&&i%10==0){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
            System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
            if (isOver(i)){
                break;
            }
        }
    }

    private synchronized boolean isOver(int step){

        if (winner!=null){
            return true;
        }
        if (step==100){
            winner=Thread.currentThread().getName();
            System.out.println("winner="+winner);
            return true;
        }


        return false;
    }


    public static void main(String[] args) {
        Race race=new Race();
        new Thread(race,"乌龟").start();
        new Thread(race,"兔子").start();
//        new Thread(race,"大象").start();
//        new Thread(race,"老虎").start();
//        new Thread(race,"狮子").start();
    }
}

